package com.cn.web.controller.PartsManagement;

import com.alibaba.fastjson2.JSONObject;
import com.cn.bean.pojo.PartsManagement.BusinessTypeTable;
import com.cn.bean.pojo.sys.System_parameter_sub_table;
import com.cn.bean.vo.bc.UserDepartments;
import com.cn.config.config.Result;
import com.cn.service.server.PartsManagement.BusinessTypeService;
import com.cn.service.server.admin.UserDepartmentsServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/BusinessType")
@Transactional
public class BusinessTypeController {
    private final BusinessTypeService businessTypeService;
    private final UserDepartmentsServer userDepartmentsServer;

    @Autowired(required = false)
    public BusinessTypeController(BusinessTypeService businessTypeService, UserDepartmentsServer userDepartmentsServer) {
        this.businessTypeService = businessTypeService;
        this.userDepartmentsServer = userDepartmentsServer;
    }
    @RequestMapping("/select/BusinessType/All/Auto1")
    @ResponseBody
    public List<BusinessTypeTable> selectBusinessTypeAllAuto1(@RequestBody JSONObject jsonObject) {
        return businessTypeService.selectBusinessTypeAsideAuto1();
    }
    @RequestMapping("/select/BusinessType/Aside/Auto1")
    @ResponseBody
    public List<BusinessTypeTable> selectBusinessTypeAsideAuto1() {
        List<BusinessTypeTable> originalData = businessTypeService.selectBusinessTypeAsideAuto1();
        Map<Integer, BusinessTypeTable> nodeMap = new HashMap<>();

        // Step 1: 构建映射
        for (BusinessTypeTable nodeData : originalData) {
            BusinessTypeTable node = new BusinessTypeTable();
            node.setAutoid(nodeData.getAutoid());
            node.setCode(nodeData.getCode());
            node.setDescription(nodeData.getDescription());
            node.setID(nodeData.getID());
            node.setIsLeaf(nodeData.getIsLeaf());
            node.setLevel(nodeData.getLevel());
            node.setName(nodeData.getName());
            node.setLabel(nodeData.getName());
            node.setParentID(nodeData.getParentID());
            node.setStatus(nodeData.getStatus());
            nodeMap.put(node.getID(), node);
        }

        // Step 2: 构建树状结构
        for (BusinessTypeTable nodeData : originalData) {

            BusinessTypeTable node = nodeMap.get(nodeData.getID());
            if (node.getParentID() != 0) {
                BusinessTypeTable parent = nodeMap.get(node.getParentID());
                if (parent != null) {
                    parent.getChildren().add(node);
                }
            }
        }

        // Step 3: 找到根节点并返回
        List<BusinessTypeTable> roots = new ArrayList<>();
        for (BusinessTypeTable node : nodeMap.values()) {
            if (node.getParentID() == 0) {
                roots.add(node);
            }
        }

        return roots;
    }
    @RequestMapping("/select/BusinessType/All")
    @ResponseBody
    public List<BusinessTypeTable> selectBusinessTypeAll(@RequestBody JSONObject jsonObject) {
        return businessTypeService.selectBusinessTypeAside();
    }
    @RequestMapping("/select/BusinessType/Aside")
    @ResponseBody
    public List<BusinessTypeTable> selectBusinessTypeAside() {
        List<BusinessTypeTable> originalData = businessTypeService.selectBusinessTypeAside();
        Map<Integer, BusinessTypeTable> nodeMap = new HashMap<>();

        // Step 1: 构建映射
        for (BusinessTypeTable nodeData : originalData) {
            BusinessTypeTable node = new BusinessTypeTable();
            node.setAutoid(nodeData.getAutoid());
            node.setCode(nodeData.getCode());
            node.setDescription(nodeData.getDescription());
            node.setID(nodeData.getID());
            node.setIsLeaf(nodeData.getIsLeaf());
            node.setLevel(nodeData.getLevel());
            node.setName(nodeData.getName());
            node.setLabel(nodeData.getName());
            node.setParentID(nodeData.getParentID());
            node.setStatus(nodeData.getStatus());
            nodeMap.put(node.getID(), node);
        }

        // Step 2: 构建树状结构
        for (BusinessTypeTable nodeData : originalData) {

            BusinessTypeTable node = nodeMap.get(nodeData.getID());
            if (node.getParentID() != 0) {
                BusinessTypeTable parent = nodeMap.get(node.getParentID());
                if (parent != null) {
                    parent.getChildren().add(node);
                }
            }
        }

        // Step 3: 找到根节点并返回
        List<BusinessTypeTable> roots = new ArrayList<>();
        for (BusinessTypeTable node : nodeMap.values()) {
            if (node.getParentID() == 0) {
                roots.add(node);
            }
        }

        return roots;
    }
    @RequestMapping("/select/BusinessType/Aside/All")
    @ResponseBody
    public List<BusinessTypeTable> selectBusinessTypeAsideAll(@RequestBody JSONObject jsonObject) {
        Integer parentID = jsonObject.getInteger("parentID");
        List<BusinessTypeTable> businessTypeTables = businessTypeService.selectBusinessTypeAside();

        List<BusinessTypeTable> result = new ArrayList<>();
        for (BusinessTypeTable type : businessTypeTables) {
            if (type.getID() == parentID) {
                // 找到与指定parentID匹配的根节点，调用递归函数获取其所有子节点
                result.add(type);
                addChildNodes(type, businessTypeTables, result);
            }
        }

        return result;
    }

    // 递归函数用于获取指定parentID下的所有子节点
    private void addChildNodes(BusinessTypeTable parent, List<BusinessTypeTable> allNodes, List<BusinessTypeTable> result) {
        for (BusinessTypeTable node : allNodes) {
            if (node.getParentID() == parent.getID()) {
                // 如果当前节点是指定parentID的子节点，将其添加到结果列表中
                result.add(node);
                // 继续递归查找当前节点的子节点
                addChildNodes(node, allNodes, result);
            }
        }
    }

    @RequestMapping("/select/BusinessType")
    @ResponseBody
    public List<BusinessTypeTable> selectBusinessType(@RequestBody JSONObject jsonObject) {
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
//        List<BusinessTypeTable> businessTypeTables = businessTypeService.selectBusinessType(limit, page);
        List<BusinessTypeTable> businessTypeTables1 = businessTypeService.selectBusinessTypeAside();
        return businessTypeTables1;
    }

    @RequestMapping("/select/System_parameter_sub_table")
    @ResponseBody
    public List<System_parameter_sub_table> selectSystem_parameter_sub_table(@RequestBody JSONObject jsonObject) {
        String tableName = jsonObject.getString("tableName");
        String field = jsonObject.getString("field");
        return businessTypeService.selectSystem_parameter_sub_table(tableName, field);
    }

    @RequestMapping("/select/UserDepartments")
    @ResponseBody
    public List<UserDepartments> selectUserDepartments(@RequestBody JSONObject jsonObject) {
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        String username = jsonObject.getString("input1");
        String name = jsonObject.getString("input2");
        username = (username != null) ? username : "";
        name = (name != null) ? name : "";
        return userDepartmentsServer.departmentsList(limit, page, username, name);
    }

    @RequestMapping("/add/BusinessType")
    @ResponseBody
    public Result addBusinessType(@RequestBody JSONObject jsonObject) {
        BusinessTypeTable formAsid = jsonObject.getObject("formAsid", BusinessTypeTable.class);
        if (formAsid.getParentID() == 0) {
            formAsid.setLevel(1);
            formAsid.setParentID(1);
        }

        Result result = businessTypeService.addBusinessType(formAsid.getName(), formAsid.getDescription(), formAsid.getStatus(), formAsid.getLevel(), formAsid.getParentID(), 0);
//        return businessTypeService.addBusinessType();
        return result;
    }

    @RequestMapping("/delete/BusinessType")
    @ResponseBody
    public Result deleteBusinessType(@RequestBody JSONObject jsonObject) {
        BusinessTypeTable formAsid = jsonObject.getObject("formAsid", BusinessTypeTable.class);
        if (formAsid.getID()==0||formAsid.getID()==1){
            return Result.failure("业务类型字段不可修改");
        }
        int id = formAsid.getID();
        return businessTypeService.deleteBusinessType(id);
    }

    @RequestMapping("/update/BusinessType")
    @ResponseBody
    public Result updateBusinessType(@RequestBody JSONObject jsonObject) {
        BusinessTypeTable formAsid = jsonObject.getObject("formAsid", BusinessTypeTable.class);
        if (formAsid.getID()==0||formAsid.getID()==1){
            return Result.failure("业务类型字段不可删除");
        }
        Result result = businessTypeService.upDateBusinessType(formAsid.getID(), formAsid.getCode(), formAsid.getName(), formAsid.getDescription(), formAsid.getStatus(), formAsid.getLevel(), formAsid.getParentID(), 0);
        return result;
    }
}
